package com.juli.school.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.juli.school.dao.entity.PsychTest;
import com.juli.school.dao.entity.TestScope;
import com.juli.school.service.bo.PsychTestBo;
import com.juli.school.service.bo.TestSubjectBo;
import com.juli.school.service.impl.PsychTestService;
import com.juli.school.service.impl.TestScopeService;
import com.juli.school.service.impl.TestSubjectService;
import com.juli.school.utils.core.model.CommonResp;

/**
 * 心理测试
 * @author: hantao
 * @since: 2017年11月6日 
 */
@RequestMapping("psychTest")
@Controller
public class PsychTestController {

	@Resource
	private PsychTestService psychTestService;
	@Resource
	private TestSubjectService testSubjectService;
	@Resource
	private TestScopeService testScopeService;
	/**
	 * 
	 * @return
	 */
	@RequestMapping("TestdetailUI")
	public String index(String snStamp,Integer num, ModelMap model){
		 CommonResp<List<TestSubjectBo>> listPage = testSubjectService.listPage(snStamp);
		 CommonResp<PsychTest> PsychTest = psychTestService.detail(snStamp);
		 model.put("length", listPage.getData()==null?"0":listPage.getData().size());
		 model.put("PsychTest", PsychTest.getData());
		 model.put("snStamp", snStamp);
		 model.put("num", num);
		 return "/test/index";
	}
	
	/**
	 * @param snStamp
	 * @param num 题目序号
	 * @param answers 选中答案
	 * @param model
	 * @return
	 */
	@RequestMapping("ajaxTestdetail")
	public String ajaxTestdetail(String snStamp,Integer num ,String answers, ModelMap model){
		CommonResp<List<TestSubjectBo>> listPage = testSubjectService.listPage(snStamp);
		if(StringUtils.isNotBlank(answers)){
			String[] split = answers.split("\\+");
			List<String> list = Arrays.asList(split);
			model.put("list", list);
		}
		model.put("obj", listPage.getData().get(num-1));
		return "/test/_testdetail";
	}
	 
	@RequestMapping("detailTest")
	private String detail(String snStamp,ModelMap model){
		CommonResp<List<TestSubjectBo>> listPage = testSubjectService.listPage(snStamp);
		model.put("testSubjext", listPage.getData());
		model.put("snStamp", snStamp);
		return "/detail/index";
	}
	
	@RequestMapping(value = "submitTest", method = RequestMethod.POST)
	@ResponseBody
	public TestScope submitTest(String snStamp,String[] answers){
		Integer aggregateScore = 0;
		for (String str : answers) {
			String[] multiple = str.split("\\+");
			for (String answer : multiple) {
				String score = null;
				if(answer.split("；").length>1){
					score = answer.split("；")[1];
				}else{
					score = answer.split("\\;")[1];
				}
				String regEx="[^0-9]";
				Pattern p = Pattern.compile(regEx);
				Matcher m = p.matcher(score);
				aggregateScore += Integer.valueOf(m.replaceAll("").trim());
			}
		}
		TestScope TestScope = testScopeService.detail(snStamp, aggregateScore.toString()).getData();
		return TestScope;
	}
	
	/**  
	 * @Description： 添加一条心理测试数据
	 * @author: hantao
	 * @since: 2017年11月7日 下午10:10:38  
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public CommonResp<Boolean> add(PsychTestBo psychTestBo){
		return psychTestService.add(psychTestBo);
	}
	
	/**  
	 * @Description： 删除一条心理测试数据
	 * @author: hantao
	 * @since: 2017年11月7日 下午10:34:02  
	 */
	@RequestMapping("delete")
	@ResponseBody
	public CommonResp<Boolean> delete(String snStamp) {
		return psychTestService.delete(snStamp);
	}
	
	/**  
	 * @Description： 分页查询所有心理测试列表
	 * @author: hantao
	 * @since: 2017年11月8日 下午10:25:49  
	 */
	@RequestMapping(value = "listPage")
	@ResponseBody
	public CommonResp<List<PsychTestBo>> listPage(Integer pageNo) {
		return psychTestService.listPage(pageNo);
	}
	
	/**  
	 * @Description： 根据编号获取心理测试详情
	 * @author: hantao
	 * @since: 2017年10月23日 下午11:05:36  
	 */
	@RequestMapping("/detail")
	@ResponseBody
	public CommonResp<PsychTest> detail(String snStamp){
		return psychTestService.detail(snStamp);
	}
	
	/**
	 * 修改一条心理测试
	 * 
	 * @param psychTestBo
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public CommonResp<Boolean> update(PsychTestBo psychTestBo) {
		return psychTestService.update(psychTestBo);
	}
}
